package com.example.leetcode.trainingcamp.week6.Sunday;

/**
 * 输入一棵二叉树的根节点，判断该树是不是平衡二叉树。如果某二叉树中任意节点的左右子树的深度相差不超过1，那么它就是一棵平衡二叉树。
 *
 *
 */
public class Offer55_II {

    boolean is = true;

    public boolean isBalanced(TreeNode root) {
        height(root);
        return is;
    }


    public int height(TreeNode root){
        if (root == null) return 0;
        int left = height(root.left);
        int right = height(root.right);
        if(!is) return 0;
        if (Math.abs(left - right)>1) is = false;
        return Math.max(height(root.left),height(root.right))+1;
    }
}
